﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.IO;
using System.Data;
using System.Xml;

namespace EncuentraWEB
{
    public class ClsXml
    {
        public string NombreArchivo { get; set; }
        public string ElementoInicial { get; set; }
        public string Elemento { get; set; }
        public void GenerarArchivo(DataTable dt)
        {
            var sw = new StreamWriter(NombreArchivo);
            var sxml = new XmlTextWriter(sw);
            sxml.Indentation = 4;
            sxml.WriteStartDocument(true);
            sxml.WriteStartElement(ElementoInicial);
            DataColumnCollection dts = dt.Columns;
            foreach (DataRow dr in  dt.Rows)
            {
                CrearElemento(sxml, dts, dr, Elemento);
            }
            sxml.WriteEndElement();
            sxml.WriteEndDocument();
            sxml.Flush();
            sxml.Close();

        }
        private static void CrearElemento(XmlTextWriter sxml, DataColumnCollection dts, DataRow dr, string element)
        {
            sxml.WriteStartElement(element);
            foreach (DataColumn dc in  dts)
            {

                if (dc.ColumnName == "decLatitud" || dc.ColumnName == "decLongitud")
                {
                    sxml.WriteAttributeString(dc.ColumnName, dr[dc.ColumnName].ToString().Replace(",","."));
                }
                else
                {
                    sxml.WriteAttributeString(dc.ColumnName, dr[dc.ColumnName].ToString());   
                }
                
            }
            sxml.WriteEndElement();
        }
    }
}
